package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.dao.SurveyDepotItemDAO;
import cj.web.admin.dao.SurveyDepotSubjectDAO;
import cj.web.admin.dao.SurveyDepotTypeDAO;
import cj.web.admin.domain.SurveyDepotSubject;
import cj.web.admin.domain.SurveyDepotType;
import cj.web.constants.MemuEnum;
import cj.web.constants.SurveyTypeEnum;
import cj.web.login.controllers.BaseController;

/**
 * 题库
 * @author Administrator
 *
 */
@Path("/surveyDepot")
public class SurveyDepotController extends BaseController{

	private static Logger logger = Logger.getLogger(SurveyDepotController.class.getName());
	
	@Autowired
	private SurveyDepotItemDAO surveyDepotItemDao;
	@Autowired
	private SurveyDepotSubjectDAO surveyDepotSubjectDao;
	@Autowired
	private SurveyDepotTypeDAO surveyDepotTypeDAO;
	
	@Get("show")
	public String show(Model model, HttpServletRequest request) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgtk);
		return "/views/survey/surveyDepotList.vm";
	}

	@Post("getList")
	public String queryList(SurveyDepotType survey) {
	
		List<SurveyDepotType> arr = surveyDepotTypeDAO.query(survey);
		
		JSONObject jo = new JSONObject();
		
		int size = surveyDepotTypeDAO.querysize(survey);
		
		jo.put("data", arr);
		jo.put("draw", survey.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgtk);
		return "/views/survey/surveyDepotDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,SurveyDepotType survey,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			survey.setCreateUser(returnSessionObj(request).getAccount());
			survey.setUpdateUser(returnSessionObj(request).getAccount());
			surveyDepotTypeDAO.create(survey);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		
		SurveyDepotType survey =surveyDepotTypeDAO.queryById(id);
		
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgtk);
        model.add("obj",survey);
		return "/views/survey/surveyDepotEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,SurveyDepotType survey,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			survey.setUpdateUser(returnSessionObj(request).getAccount());
			surveyDepotTypeDAO.update(survey);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id){
		surveyDepotTypeDAO.deleteById(id);
		return "r:show";
	}
	
	//--------surveySubject--------
	@Get("showSj")
	public String showSubject(Model model,@Param("id") int id, HttpServletRequest request) {
		SurveyDepotType survey =surveyDepotTypeDAO.queryById(id);
		//MemuEnum
		model.add("surveyDepotId",id);
		model.add("vote",survey);
        model.add("MemuEnum",MemuEnum.lkgtk);
        model.add("isMutilEnum",EnumSet.allOf(SurveyTypeEnum.class));
		return "/views/survey/surveyDepotSubjectList.vm";
	}

	@Post("getSjList")
	public String querySubjectList(SurveyDepotSubject surveySubject) {
		JSONObject jo = new JSONObject();
		List<Map> arr = surveyDepotSubjectDao.query(surveySubject);
		int size = surveyDepotSubjectDao.querysize(surveySubject);
		
		jo.put("data", arr);
		jo.put("draw", surveySubject.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("createSjPage")
	public String createSubjectPage(Model model,@Param("surveyDepotId") int surveyDepotId){
		//MemuEnum
		model.add("surveyDepotId",surveyDepotId);
        model.add("MemuEnum",MemuEnum.lkgtk);
        model.add("SurveyTypeEnum",EnumSet.allOf(SurveyTypeEnum.class));
		return "/views/survey/surveyDepotSubjectDetail.vm";
	}
	
	@Post("createSj")
	public String createSubject(Model model, @Param("surveyDepotId")int surveyDepotId, @Param("isMutil")int isMutil, 
			@Param("subject")String subject, @Param("answer")List<Integer> answers, @Param("item")List<String> items, @Param("count")int count, HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		if(isMutil == 2){
			items.add("正确");
			items.add("错误");
		}
		
		try {
			String l_answers = "";//存放考试头答案信息
			String user = returnSessionObj(request).getAccount();
			int subjectId = surveyDepotSubjectDao.create(surveyDepotId, isMutil, subject, user,count);
			
			for(int i = 0; i < items.size(); i++){
				String item = items.get(i);
				int answer = answers.get(i).intValue();
				int surveyDepotItemId = surveyDepotItemDao.create(surveyDepotId, subjectId, item, user, i, answer);
				
				if(answer==1){
					l_answers+=surveyDepotItemId+"@";
				}
			}
			//更新答案
			surveyDepotSubjectDao.update(subjectId, l_answers);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("editSj")
	public String editSubject(Model model,@Param("id") int id){
		
		SurveyDepotSubject surveySubject =surveyDepotSubjectDao.queryById(id);
		int subjectDepotId = surveySubject.getId().intValue();
		List<Map> items = surveyDepotItemDao.queryBySubjectId(subjectDepotId);
		
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgtk);
        model.add("SurveyTypeEnum",EnumSet.allOf(SurveyTypeEnum.class));
        model.add("Subject",surveySubject);
        model.add("Items",items);
		return "/views/survey/surveyDepotSubjectEdit.vm";
	}
	
	@Post("updateSj")
	public String updateSubject(Model model, @Param("subjectDepotId")int subjectDepotId, @Param("isMutil")int isMutil, 
			@Param("subject")String subject, @Param("id")List<Integer> ids, @Param("item")List<String> items, @Param("answer")List<Integer> answers, @Param("count")int count, 
			HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		if(isMutil == 2){
			items.add("正确");
			items.add("错误");
		}
		String user = returnSessionObj(request).getAccount();
		SurveyDepotSubject origSub =surveyDepotSubjectDao.queryById(subjectDepotId);
		
		surveyDepotItemDao.deleteBySubId(subjectDepotId); //逻辑删除所有原item
		
		try {
			surveyDepotSubjectDao.update(subjectDepotId, isMutil, subject, user, count);
			if( null != items && items.size() > 0 ){
				for(int i = 0; i < items.size(); i++){
					String item = items.get(i);
					int itemId = ids.get(i).intValue();
					if(itemId > -1){
						surveyDepotItemDao.update(itemId, item, user, i, answers.get(i).intValue());
					}
					else{
						surveyDepotItemDao.create(origSub.getSurveyDepotId(), subjectDepotId, item, user, i, answers.get(i).intValue());
					}
				}
			}
			
			String l_answers = "";//存放考试头答案信息
			List<Integer> surveyDepotItemIds = surveyDepotItemDao.queryIdBySubjectId(subjectDepotId);
			if(surveyDepotItemIds!=null && surveyDepotItemIds.size()>0){
				for(Integer surveyDepotItemId:surveyDepotItemIds){
					l_answers+=surveyDepotItemId+"@";
				}
			}
			//更新答案
			surveyDepotSubjectDao.update(subjectDepotId, l_answers);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteSjById")
	public String deleteSubjectById(Model model,@Param("id") int id){
		
		SurveyDepotSubject surveySubject =surveyDepotSubjectDao.queryById(id);
		
		if(surveySubject!=null)
		{
			surveyDepotSubjectDao.deleteById(id);
		}
		
		return "r:showSj?id="+surveySubject.getSurveyDepotId();
	}
}
